package com.wwmz.mebatis.v2.session;


import com.wwmz.mebatis.v2.executor.Executor;

/**
 * Created by wumz on 2020/10/17 11:04
 */
public class DefaultSqlSession {

    private Configuration configuration;

    private Executor executor;

    public DefaultSqlSession(Configuration configuration){
        this.configuration = configuration;
        // 根据全局配置决定是否使用缓存装饰
        this.executor = configuration.newExecutor();
    }

    public Configuration getConfiguration() {
        return configuration;
    }

    public <T> T getMapper(Class<T> clazz) {
        return configuration.getMapper(clazz,this);
    }

    public <T> T selectOne(String statementId, Object[] args, Class object) {
        String sql = getConfiguration().getMappedStatement(statementId);
        //打印代理对象时会自动调用toString()方法，触发invoke()
        return executor.query(sql, args, object);
    }
}
